// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//

#include <cantor/parser/parser.h>
#include <cantor/transform/stmts/create_table_stmt.h>
#include <cantor/transform/clause/table_name.h>
#include <cantor/transform/ddls/column_def.h>
#include <cantor/transform/ddls/constraint.h>
#include <cantor/transform/ddls/table_option.h>

namespace cantor::transform {

    void TransCreateTableStmt::resolve_ast(const cantor::parser::CreateTableStmt*node) {
        if (!node) {
            status = turbo::data_loss_error("no expr");
            return;
        }
        if (!node->table_name) {
            status = turbo::data_loss_error("no table name");
        }
        /// if_not_exist
        create_table_stmt.set_if_not_exist(node->if_not_exist);

        status = TransTableName::resolve_to(node->table_name, create_table_stmt.mutable_table_name());
        if (!status.ok()) {
            return;
        }
        if (node->ref_table_name) {
            status = TransTableName::resolve_to(node->ref_table_name, create_table_stmt.mutable_ref_table_name());
            if (!status.ok()) {
                return;
            }
        }

        auto cols = create_table_stmt.mutable_columns();
        cols->Reserve(node->columns.size());
        for (size_t i = 0; i < node->columns.size(); ++i) {
            status = TransColumnDef::resolve_to(node->columns[i], cols->Add());
            if (!status.ok()) {
                return;
            }
        }

        auto constraints = create_table_stmt.mutable_constraints();
        constraints->Reserve(node->constraints.size());
        for (size_t i = 0; i < node->constraints.size(); ++i) {
            status = TransConstraint::resolve_to(node->constraints[i], constraints->Add());
            if (!status.ok()) {
                return;
            }
        }

        auto options = create_table_stmt.mutable_options();
        options->Reserve(node->options.size());
        for (size_t i = 0; i < node->options.size(); ++i) {
            status = TransTableOption::resolve_to(node->options[i], options->Add());
            if (!status.ok()) {
                return;
            }
        }

        set_proto_location(node, create_table_stmt.mutable_location());
        set_proto_status(create_table_stmt.mutable_status());
    }

    const turbo::Status &TransCreateTableStmt::check() {
        if (!status.ok()) {
            return status;
        }
        if (!create_table_stmt.has_location()) {
            make_missing_required_field("location");
        }
        return status;
    }

    turbo::Status TransCreateTableStmt::resolve_to(turbo::Nonnull<const cantor::parser::CreateTableStmt*> arg, turbo::Nonnull<pb::ast::CreateTableStmt *>out) {
        TransCreateTableStmt trans;
        trans.resolve_ast(arg);
        auto status = trans.check();
        if (!status.ok()) {
            return status;
        }
        *out = std::move(trans.create_table_stmt);

        return status;
    }
}  // namespace cantor::transform
